package com.ypj.indusynergyems.controller;

import com.ypj.indusynergyems.dao.entity.CategoryEntity;
import com.ypj.indusynergyems.dao.entity.GoodsEntity;
import com.ypj.indusynergyems.dao.entity.GoodsTypeEntity;
import com.ypj.indusynergyems.service.CategoryService;
import com.ypj.indusynergyems.service.IGoodsService;
import com.ypj.indusynergyems.service.IShopService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequiredArgsConstructor
public class GoodsController {

    private final CategoryService categoryService;

    private final IShopService shopService;

    private final IGoodsService goodsService;

    //添加商品

    /**
     * 查找商品大类/查找店铺商品自定义类型返回给前端选择
     */
    @GetMapping("/system/shop/goodsAdd/{shopId}")
    public String findAllType(@PathVariable("shopId") int shopId, Model model){
        //查询一级类型
        List<CategoryEntity> firstTypes = categoryService.getLevel1Categorys();
        //查询二级类型
        List<CategoryEntity> secondTypes = categoryService.getLevel2Categorys();
        //查询三级类型
        List<CategoryEntity> thirdTypes = categoryService.getLevel3Categorys();

        model.addAttribute("firstTypes",firstTypes);
        model.addAttribute("secondTypes",secondTypes);
        model.addAttribute("thirdTypes",thirdTypes);

        //查询店铺自定义商品类型
        List<GoodsTypeEntity> allShopType = shopService.findAllShopType(shopId);
        model.addAttribute("allShopType",allShopType);
        return "/system/shop/goodsAdd";
    }


    /**
     * 添加商品
     */
    @PostMapping("/system/shop/goods/add")
    public @ResponseBody String saveGoods(GoodsEntity goodsEntity){
        return goodsService.addGoods(goodsEntity);
    }



}
